﻿using DTcms.Common;
using DTcms.Web.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Web.Http;

namespace DTcms.Web.Plugin.Controllers
{
    public class feedbackController : WebApiController
    {
        Model.feedbackconfig config = new BLL.feedbackconfig().loadConfig("/plugins/feedback/config/install.config");

        #region 发布留言================================
        [HttpPost]
        public IHttpActionResult add(int site, string txtContact, string txtMobile, string txtShortDesc, string txtVerCode)
        {
            BLL.plugin_feedback bll = new BLL.plugin_feedback();
            Model.plugin_feedback model = new Model.plugin_feedback();
            
            #region 验证
            //检查站点目录
            if (site == 0)
            {
                return Json(new { status = 0, msg = "对不起，网站传输参数有误！" });
            }
            //校检验证码
            string result1 = verify_code(txtVerCode);
            if (result1 != "success")
            {
                return Json(new { status = 0, msg = result1 });
            }
            if (string.IsNullOrEmpty(txtShortDesc))
            {
                return Json(new { status = 0, msg = "对不起，请输入留言的内容！" });
            }
            if (string.IsNullOrEmpty(txtContact))
            {
                return Json(new { status = 0, msg = "对不起，请输入您的称呼！" });
            }
            #endregion

            if (userModel != null)
            {
                if (string.IsNullOrEmpty(txtContact))
                {
                    txtContact = userModel.user_name;
                }
                if (string.IsNullOrEmpty(txtMobile))
                {
                    txtMobile = userModel.mobile;
                }
            }
            model.site_id = site;
            model.system = DTRequest.GetPhoneType();
            model.city = DTRequest.GetCity();
            model.title = string.Format("来自{0}的留言", txtContact);
            model.content = txtShortDesc;
            model.user_name = txtContact;
            model.user_tel = txtMobile;
            model.add_time = DateTime.Now;
            model.is_lock = siteConfig.commentstatus; //不需要审核，请改为0

            if (bll.Add(model) > 0)
            {
                //是否开启通知功能
                if (config.bookmsg > 0 && config.receive != "")
                {
                    switch (config.bookmsg)
                    {
                        case 1:
                            Model.sms_template smsModel = new BLL.sms_template().GetModel("call_index='" + config.booktemplet + "'", " * ", ""); //取得短信内容
                            if (smsModel != null)
                            {
                                //替换模板内容
                                string smstxt = smsModel.content;
                                smstxt = smstxt.Replace("{webname}", siteConfig.webname);
                                smstxt = smstxt.Replace("{webtel}", siteConfig.webtel);
                                smstxt = smstxt.Replace("{weburl}", siteConfig.weburl);
                                smstxt = smstxt.Replace("{username}", model.user_name);
                                smstxt = smstxt.Replace("{usertel}", model.user_tel);
                                smstxt = smstxt.Replace("{userqq}", model.user_qq);
                                smstxt = smstxt.Replace("{useremail}", model.user_email);
                                smstxt = smstxt.Replace("{usertitle}", model.title);
                                smstxt = smstxt.Replace("{usercontent}", model.content);
                                //发送短信
                                string tipMsg = string.Empty;
                                bool result = new BLL.sms_message().Send(config.receive, smstxt, 1, out tipMsg);
                                if (!result)
                                {
                                    LogHelper.WriteLog("手机信息发送失败!");
                                }
                            }
                            break;
                        case 2:
                            //获得邮件内容
                            Model.mail_template mailModel = new BLL.mail_template().GetModel("call_index='" + config.booktemplet + "'", " * ", ""); //取得邮件内容
                            if (mailModel != null)
                            {
                                //替换模板内容
                                string titletxt = mailModel.maill_title;
                                string bodytxt = mailModel.content;

                                titletxt = titletxt.Replace("{webname}", siteConfig.webname);
                                titletxt = titletxt.Replace("{username}", model.user_name);

                                bodytxt = bodytxt.Replace("{webname}", siteConfig.webname);
                                bodytxt = bodytxt.Replace("{webtel}", siteConfig.webtel);
                                bodytxt = bodytxt.Replace("{weburl}", siteConfig.weburl);

                                bodytxt = bodytxt.Replace("{username}", model.user_name);
                                bodytxt = bodytxt.Replace("{usertel}", model.user_tel);
                                bodytxt = bodytxt.Replace("{userqq}", model.user_qq);
                                bodytxt = bodytxt.Replace("{useremail}", model.user_email);
                                bodytxt = bodytxt.Replace("{usertitle}", model.title);
                                bodytxt = bodytxt.Replace("{usercontent}", model.content);
                                //循环发送
                                string[] emailArr = config.receive.Split(',');
                                foreach (string email in emailArr)
                                {
                                    if (Utils.IsValidEmail(email))
                                    {
                                        //发送邮件
                                        try
                                        {
                                            DTMail.sendMail(siteConfig.emailport, siteConfig.emailsmtp, siteConfig.emailssl,
                                                siteConfig.emailusername,
                                                DESEncrypt.Decrypt(siteConfig.emailpassword, siteConfig.sysencryptstring),
                                                siteConfig.emailnickname,
                                                siteConfig.emailfrom,
                                                email,
                                                titletxt, bodytxt);
                                        }
                                        catch
                                        {
                                            LogHelper.WriteLog("邮件发送失败!");
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
                return Json(new { status = 1, msg = "恭喜您，留言提交成功！" });
            }
            return Json(new { status = 0, msg = "对不起，保存过程中发生错误！" });
        }
        #endregion

        #region 获取留言================================
        [HttpPost]
        public IHttpActionResult feedback_list(int page = 1, int size = 10)
        {
            string site_path = Utils.SafeXXS(DTRequest.GetQueryString("site").Trim());

            if (string.IsNullOrWhiteSpace(site_path))
            {
                return Json(new { status = 0, msg = "出错了，参数错误！" });
            }

            var resultlist = new BLL.plugin_feedback().GetQueryPage(page, size, "is_lock=0 and site_path='" + site_path + "'", "add_time desc");
            List<Model.plugin_feedback> list = resultlist.Items;
            return Json(new { status = 1, msg = "获取留言列表成功！", total = resultlist.TotalItems, list = list });
        }
        #endregion
    }
}